import requests
import re
headers = {
    'authority': 'weibo.com',
    'x-requested-with': 'XMLHttpRequest',
    'sec-ch-ua-mobile': '?0',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
    'content-type': 'application/x-www-form-urlencoded',
    'accept': '*/*',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'cors',
    'sec-fetch-dest': 'empty',
    'referer': 'https://weibo.com/1192329374/KnnG78Yf3?filter=hot&root_comment_id=0&type=comment',
    'accept-language': 'zh-CN,zh;q=0.9,en-CN;q=0.8,en;q=0.7,es-MX;q=0.6,es;q=0.5',
    'cookie': 'SCF=Ang1PsM9AoPOH6r2JMfsf_cG-DF5ce8cSUt-u4AhK1i5wQRp7tw8QyalBIidf_baiVUx4v-heMf80G-h4cl7hGc.; '
              'UOR=www.baidu.com,weibo.com,www.baidu.com; SINAGLOBAL=6060355668604.674.1709552585531; '
              'ULV=1709552585538:1:1:1:6060355668604.674.1709552585531:; XSRF-TOKEN=Up1y5N9BMS-OEZE2f1nAmtO6; '
              'ALF=1712824665; SUB=_2A25I9GQJDeRhGeNI7FEQ-CbIyD2IHXVriPnBrDV8PUJbkNB-LXjhkW1NSCMrRn'
              '-09qYuh7cnbvie_tTPr1BM4rl5; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WWSJ_7WUJKD_-HWMMWH1YY_5JpX5KMhUgL'
              '.Fo-cS0ep1hnXe022dJLoIEBLxKqL1KnL1-qLxK-L1h-L1h.LxK-LB.-LB--LxKqLB-BLBK-t; '
              'WBPSESS=DZfzo8PBycpQXciQLyzIdlhjpu3GSrUjHdDzGdeTxn6_FTe0MIZZN5H_tmED0x6_DyEheg9EuSsRBmW52A16epCDHtMn95z8untpF5c1czvTkOBDuN6f0LIVsm6ZiF-rygc-UjWhLBHxdfhdrdJ0zA=='
}

def parseUid(uid):
    response = requests.get(url=f'https://weibo.com/ajax/profile/info?custom={uid}',headers=headers)
    try:
        return response.json()['data']['user']['id']
    except:
        return None
def getUserInfo(uid):
    try:
        uid = int(uid)
    except:
        # 说明是 xiena 这样的英文串
        uid = parseUid(uid)
        if not uid:
            return None
    response = requests.get(url=f'https://weibo.com/ajax/profile/detail?uid={uid}', headers=headers)
    if response.status_code == 400:
        return {
            'errorMsg': '用户可能注销或者封号',
            'location': None,
            'user_link': f'https://weibo.com/{uid}'
        }
    resp_json = response.json().get('data', None)
    if not resp_json:
        return None
    sunshine_credit = resp_json.get('sunshine_credit', None)
    if sunshine_credit:
        sunshine_credit_level = sunshine_credit.get('level', None)
    else:
        sunshine_credit_level = None
    education = resp_json.get('education', None)
    if education:
        school = education.get('school', None)
    else:
        school = None
    ip_location = resp_json.get('ip_location', None)
    location = resp_json.get('location', None)
    gender = resp_json.get('gender', None)

    birthday = resp_json.get('birthday', None)
    created_at = resp_json.get('created_at', None)
    description = resp_json.get('description', None)
    # 我关注的人中有多少人关注 ta
    followers = resp_json.get('followers', None)
    if followers:
        followers_num = followers.get('total_number', None)
    else:
        followers_num = None
    return {
        'sunshine_credit_level': sunshine_credit_level,
        'school': school,
        'location': location,
        'gender': gender,
        'birthday': birthday,
        'created_at': created_at,
        'description': description,
        'followers_num': followers_num,
        'ip_location': ip_location
    }

def getIPlocation(uid):
    response = requests.get(url=f'https://weibo.com/ajax/profile/detail?uid={uid}',headers=headers)
    resp_json = response.json().get('data', None)
    if not resp_json:
        return None
    ip_location = resp_json.get('ip_location', None)
    ip_locationRes = ''
    if '：' in ip_location:
        ip_locationRes = ip_location.split('：')[1]
    else:
        ip_locationRes = ip_location
    return ip_locationRes
import time
start_time1 = time.time()
print(getUserInfo(3626485974))
end_time1 = time.time()
print(end_time1 - start_time1)
start_time = time.time()
print(getIPlocation(1712539910))
end_time = time.time()
print(end_time - start_time)
